package com.elong.businesstravel.modules.member;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

import com.android.support.jhf.androidpulltorefresh.library.PullToRefreshBase;
import com.android.support.jhf.androidpulltorefresh.library.PullToRefreshListView;
import com.android.support.jhf.androidpulltorefresh.library.PullToRefreshBase.Mode;
import com.android.support.jhf.androidpulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.android.support.jhf.handlerui.HandlerPostUI;
import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.network.NetworkClient;
import com.android.support.jhf.network.loopj.android.http.ResponseHandlerInterface;
import com.elong.businesstravel.R;
import com.elong.businesstravel.base.baseactivity.BaseActivity;
import com.elong.businesstravel.base.view.TitleNavBarView;
import com.elong.businesstravel.entitys.AccountInfo;
import com.elong.businesstravel.modules.member.adapter.MemberGetBonusAdapter;
import com.elong.businesstravel.modules.member.adapter.MemberGetUseBonusAdapter;
import com.elong.businesstravel.protocol.login.MemberGetUseBonusRequest;
import com.elong.businesstravel.protocol.login.MemberGetUseBonusResponse;
/**
 * 使用记录
 * @author lenovo
 *
 */
public class MemberGetUseBonusActivity extends BaseActivity {
	
	private PullToRefreshListView mPullToRefreshListView;
	private MemberGetUseBonusAdapter mMemberGetUseBonusAdapter;

	private List<AccountInfo> mMemberGetUseBonus = new ArrayList<AccountInfo>();
	private int mPage = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_member_get_user_bonus);
		
		init();
	}
	
	@Override
	protected void init() {
		
		mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.listView);
		mPullToRefreshListView.setMode(Mode.PULL_FROM_END);
		mMemberGetUseBonusAdapter = new MemberGetUseBonusAdapter(mContext, mMemberGetUseBonus);
		mPullToRefreshListView.setAdapter(mMemberGetUseBonusAdapter);

		mPullToRefreshListView
				.setOnRefreshListener(new OnRefreshListener<ListView>() {

					@Override
					public void onRefresh(
							PullToRefreshBase<ListView> refreshView) {
						// 更多
						memberGetUseBonus(true);

						HandlerPostUI.getHandlerPostUI(new Runnable() {

							@Override
							public void run() {
								mPullToRefreshListView.onRefreshComplete();
							}
						});
					}
				});
		
		setTitleNavBar();
		
		memberGetUseBonus(false);
	}
	
	@Override
	protected void setTitleNavBar() {
		TitleNavBarView titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage("使用记录");
		titleNavBarView.setLeftDrawableImage(R.drawable.btn_title_back);
		titleNavBarView.setLeftButtonImageString(0, "返回", R.color.text_blue);
		titleNavBarView.setLeftButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		titleNavBarView.setRightButtonVisibility(View.INVISIBLE);
	}
	
	/**
	 * 使用记录
	 */
	private void memberGetUseBonus(final boolean moreFlag) {
		
		if (moreFlag) {
			mPage++;
		} else {
			mPage = 1;
		}
		
		NetworkClient.getNetworkClient().PostRequest(
				new MemberGetUseBonusRequest(mContext, mPage + ""),
				new MemberGetUseBonusResponse(){
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						HandlerPostUI.getHandlerPostUI(new Runnable() {

							@Override
							public void run() {
								if (moreFlag) {
									// 更多
									if (0 == accountInfos.size()) {
										HandlerToastUI.getHandlerToastUI(
												mContext, "没有更多信息");
									} else {
										mMemberGetUseBonus.addAll(accountInfos);
									}
								} else {
									// 刷新
									mMemberGetUseBonus.clear();
									mMemberGetUseBonus.addAll(accountInfos);
								}
								mMemberGetUseBonusAdapter.notifyDataSetChanged();
							}
						});
					}
				});
	}
	
}
